package com.spzx.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.api.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.ICategoryBrandService;
import com.spzx.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 分类品牌Service业务层处理
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {

    @Autowired
    private CategoryBrandMapper categoryBrandMapper;

    @Autowired
    private ICategoryService categoryService;

    @Override
    public List<CategoryBrand> selectCategoryBrandList(CategoryBrand categoryBrand) {
        return categoryBrandMapper.selectCategoryBrandList(categoryBrand);
    }

    @Override
    public CategoryBrand selectCategoryBrandById(Long id) {
        CategoryBrand categoryBrand = categoryBrandMapper.selectById(id);

        //解决分类下拉回显问题。借助于ICategoryService接口getAllParentId()方法，获取数据。例如：传递三级分类id=3,把三个级别分类id都获取出来[1,2,3],并且按照顺序封装。
        List<Long> categoryIdList = categoryService.getAllParentId(categoryBrand.getCategoryId());
        categoryBrand.setCategoryIdList(categoryIdList);

        return categoryBrand;
    }

    @Override
    public int insertCategoryBrand(CategoryBrand categoryBrand) {
        //解决问题：已经保存了分类和品牌的关系数据，就不能再次保存。否则重复。
        Long count = categoryBrandMapper.selectCount(
                new LambdaQueryWrapper<CategoryBrand>()
                        .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
                        .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId()));
        if (count > 0) { //说明数据以存在
            throw new ServiceException("品牌和分类关联已经存在");
        }
        return categoryBrandMapper.insert(categoryBrand);
    }

    @Override
    public int updateCategoryBrand(CategoryBrand categoryBrand) {
        //解决问题：
        //1.数据关联不变，无需修改。但是，修改数据的当前登录用户不同，可以更新修改人。还有remark表单如果提交也需要修改。
        /*CategoryBrand dbCategoryBrand = categoryBrandMapper.selectById(categoryBrand.getId());
        if (dbCategoryBrand.getBrandId() == categoryBrand.getBrandId() && dbCategoryBrand.getCategoryId() == categoryBrand.getCategoryId()) {
            return 1;
        }*/

        //2.修改过表单提交，数据关联也已经存在，不能修改。抛业务异常。
        Long count = categoryBrandMapper.selectCount(
                new LambdaQueryWrapper<CategoryBrand>()
                        .eq(CategoryBrand::getCategoryId, categoryBrand.getCategoryId())
                        .eq(CategoryBrand::getBrandId, categoryBrand.getBrandId())
        );
        if(count>0){
            throw new ServiceException("修改的品牌和分类关联数据已经存在");
        }

        return categoryBrandMapper.updateById(categoryBrand);
    }


    @Override
    public List<Brand> selectBrandListByCategoryId(Long categoryId) {
        return categoryBrandMapper.selectBrandListByCategoryId(categoryId);
    }
}